﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace PIMS.WorkPlugin.Util
{
    internal class ByteCode
    {
        /// <summary>
        /// 字节数组转16进制字符串
        /// </summary>
        /// <param name="bytes"></param>
        /// <returns></returns>
        public static string byteToHexStr(byte[] bytes)
        {
            string returnStr = "";
            if (bytes != null)
            {
                for (int i = 0; i < bytes.Length; i++)
                {
                    returnStr += bytes[i].ToString("X2");
                    if (i % 2 != 0) returnStr += " ";
                }
            }
            return returnStr;
        }

        /// <summary>
        /// 正则表达式验证中文字符
        /// </summary>
        /// <param name="text"></param>
        /// <returns></returns>
        public static bool JustChinese(string text = "天下英雄abc")
        {
            for (int i = 0; i < text.Length; i++)
            {
                //判断汉字的Unicode编码范围
                if (Regex.IsMatch(text[i].ToString(), @"[\u4e00-\u9fbb]"))
                    return true;
            }
            return false;
        }

        /// <summary>
        /// 通过正则判断给定的字符串是否是纯数字
        /// </summary>
        /// <param name="message"></param>
        /// <param name="length"></param>
        /// <returns></returns>
        public static bool IsNumeric(string message)
        {
            string match = @"^\d*$";
            if (message != "" && Regex.IsMatch(message, match))
            {
                //成功
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
